package com.mwh.yuantu.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mwh.yuantu.core.ErrorMsg;
import com.mwh.yuantu.core.MindriveException;
import com.mwh.yuantu.entity.CommentInfoEntity;
import com.mwh.yuantu.entity.ReplyInfoEntity;
import com.mwh.yuantu.entity.UserInfoEntity;
import com.mwh.yuantu.parameter.CommentInfoParameter;
import com.mwh.yuantu.parameter.CommentInfoSearchParameter;
import com.mwh.yuantu.dto.CommentInfoDTO;
import org.springframework.beans.BeanUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * (CommentInfo)表服务接口
 *
 * @author maweihong
 * @since 2023-11-01 09:47:30
 */
@Service
@Slf4j
public class CommentInfoService extends BaseService {

    /**
     * 保存用户评论
     * @param videoId
     * @param content
     * @param userInfo
     * @throws
     */
    public void save(String videoId, String content, UserInfoEntity userInfo) {

        // 1.0 校验评论内容
        if (StringUtils.isEmpty(content)) {
            throw new MindriveException(ErrorMsg.ERROR_900001);
        }

        // 删除普通标签
        content = content.replaceAll("<(S*?)[^>]*>.*?|<.*? />", "");
        // 删除转义字符
        content = content.replaceAll("&.{2,6}?;", "");

        // 2.0 保存评论内容
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String now = formatter.format(currentTime);

        CommentInfoEntity commentInfo = new CommentInfoEntity();
        commentInfo.setVideoId(Integer.parseInt(videoId));
        commentInfo.setUserId(userInfo.getId());
        commentInfo.setLoginName(userInfo.getLoginName());
        commentInfo.setContent(content);
        commentInfo.setUpdateTime(new Date());

        iCommentInfoDAO.insert(commentInfo);
    }

    /**
     * 获取某个视频的评论列表
     * @param videoId 视频主键
     * @return
     */
    public List<CommentInfoEntity> listByVideoId(String videoId) {
        List<CommentInfoEntity> list = iCommentInfoDAO.listByVideoId(videoId);
        if (list!=null && list.isEmpty()==false) {
            int len = list.size();
            for (int i=0; i<len; i++) {
                // 查询该评论下的回复列表
                List<ReplyInfoEntity> replyInfoLsit = iReplyInfoDAO.listByCommentId(list.get(i).getId()+"");
                list.get(i).setReplyInfoList(replyInfoLsit);
            }
        }

        return list;
    }

    /**
     * 获取某个视频的评论条数
     * @param videoId 视频id
     * @return
     */
    public int getCommentCount(String videoId) {
        // 评论条数
        int commentCount = iCommentInfoDAO.countByVideoId(videoId);
        // 回复条数
        int replyCount = iReplyInfoDAO.countByVideoId(videoId);

        return (commentCount+replyCount);
    }
}

